package com.cxz.study.application.order.web;

import com.cxz.study.application.order.web.mapper.OrderWebMapperStruct;
import com.cxz.study.application.order.web.req.CancelOrderRequest;
import com.cxz.study.application.order.web.req.CreateOrderRequest;
import com.cxz.study.application.order.web.useCase.CancelOrderUseCase;
import com.cxz.study.application.order.web.useCase.CreateOrderUseCase;
import com.cxz.study.application.order.web.useCase.FindOrderUseCase;
import com.cxz.study.common.Result;
import com.cxz.study.domain.exception.InventoryNotEnoughException;
import com.cxz.study.domain.exception.OrderNotCancelableException;
import com.cxz.study.domain.exception.OrderNotFoundException;
import com.cxz.study.domain.exception.PermissionDeniedException;
import com.cxz.study.domain.order.command.CancelOrderCommand;
import com.cxz.study.domain.order.command.CreateOrderCommand;
import com.cxz.study.domain.order.command.FindOrderCommand;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @className: OrderController
 * @author: ocxz1
 * @date: 2025/6/15 下午12:09
 * @Version: 1.0
 * @description: ddd-struct3
 */
@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {

    private static final Logger log = LoggerFactory.getLogger(OrderController.class);
    private final OrderWebMapperStruct orderWebMapperStruct;
    private final CreateOrderUseCase createOrderUseCase;
    private final CancelOrderUseCase cancelOrderUseCase;
    private final FindOrderUseCase findOrderUseCase;

    /**
     * 创建订单
     * @param request 创建订单请求
     * @return 返回创建的订单
     */
    @RequestMapping("/createOrder")
    public Result<OrderVO> createOrder(@RequestBody CreateOrderRequest request) {
        try {
            CreateOrderCommand command = orderWebMapperStruct.toCommand(request);
            OrderVO order = createOrderUseCase.execute(command);
            return Result.success(order);
        } catch (InventoryNotEnoughException e1) {
            return Result.error("InventoryNotEnough", "库存不足");
        } catch (Exception e2) {
            log.error("create order error. error msg: {}", e2.getMessage(), e2);
            return Result.error("UnknownError", "未知错误");
        }
    }

    /**
     * 取消订单
     * @param request 取消订单请求
     * @return 返回取消的订单
     */
    @RequestMapping("{orderId}/cancelOrder")
    public Result<OrderVO> cancelOrder(@PathVariable("orderId") Long orderId, @RequestBody CancelOrderRequest request) {
        CancelOrderCommand command = orderWebMapperStruct.toCommandWithOrderId(request, orderId);
        try {
            OrderVO execute = cancelOrderUseCase.execute(command);
            return Result.success(execute);
        } catch (OrderNotFoundException  e) {
            return Result.error("OrderNotFound", "订单不存在");
        } catch (PermissionDeniedException e) {
            return Result.error("PermissionDenied", "无权限");
        } catch (OrderNotCancelableException e) {
            return Result.error("OrderNotCancelable", "订单不可取消");
        } catch (Exception e) {
            return Result.error("UnknownError", "未知错误");
        }
    }

    @RequestMapping("/{orderId}/findOrder")
    public Result<OrderVO> findOrder(@PathVariable("orderId") Long orderId) {

        FindOrderCommand findOrderCommand = new FindOrderCommand();
        findOrderCommand.setOrderId(orderId);

        OrderVO execute = findOrderUseCase.execute(findOrderCommand);
        if (execute != null) {
            return Result.success(execute);
        }
        return Result.error("OrderNotFound", "订单不存在");
    }
}
